package com.zxd.service.impl;

import com.zxd.core.Constant;
import com.zxd.dao.RoleRelationDao;
import com.zxd.entity.RoleRelation;
import com.zxd.service.RoleRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * roleRelationServiceImpl
 * Created by 奕恒 on 2017/2/20 21:54.
 */
@Service
public class RoleRelationServiceImpl implements RoleRelationService {

    @Autowired
    private RoleRelationDao roleRelationDao;

    @Override
    public List<RoleRelation> findAll() {
        return roleRelationDao.findAll();
    }

    @Override
    public Page<RoleRelation> find(int pageNum, int pageSize) {
        return roleRelationDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<RoleRelation> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public RoleRelation getById(int id) {
        return roleRelationDao.findOne(id);
    }

    @Override
    public void deleteById(int id) {
        roleRelationDao.delete(id);
    }

    @Override
    public RoleRelation create(RoleRelation roleRelation) {
        return roleRelationDao.save(roleRelation);
    }

    @Override
    public RoleRelation update(RoleRelation roleRelation) {
        return roleRelationDao.save(roleRelation);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }
}